<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="page.query.customerName" placeholder="请输入客户名称" clearable />
        </el-form-item>
        <el-form-item label="客户编号" prop="customerNo">
          <el-input v-model="page.query.customerNo" placeholder="请输入客户编号" clearable />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNum">
          <el-input v-model="page.query.phoneNum" placeholder="请输入手机号" clearable />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <dict-select v-model="page.query.state" dict-key="comm_state" placeholder="请选择状态" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
<!--      <div>
        <span>客户信息列表</span>
      </div>
      <el-divider />-->
      <div class="table-button">
        <el-button
          type="primary"
          class="el-icon-plus"
          @click="saveClick"
        >新增</el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
<!--        <el-button plain type="primary" class="el-icon-upload2" @click="importClick">导入</el-button>
        <download-button :disabled="tableData.length===0" :params="downloadParam" :path="downloadApiPath" icon="el-icon-download" plain />-->
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="客户编号" prop="customerNo" />
        <el-table-column label="客户名称" prop="customerName" />
        <el-table-column label="联系人名称" prop="contacts" />
        <el-table-column label="手机号" prop="phoneNum" />
        <el-table-column label="应收款" prop="receivables">
          <template slot-scope="scope">
            <money-format v-model="scope.row.receivables" />
          </template>
        </el-table-column>
        <el-table-column label="已收款" prop="receivedPayment">
          <template slot-scope="scope">
            <money-format v-model="scope.row.receivedPayment" />
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="state">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
              @change="openOrClose(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="地址" prop="address" show-overflow-tooltip />
        <el-table-column label="操作" min-width="160" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="detailClick(scope.row)"
            >详情
            </el-button>
            <el-button
              type="text"
              @click="updateClick(scope.row)"
            >修改
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
    <div v-if="detailVisible">
      <detail-module ref="detailModule" :visible="detailVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import {
  customer_delete,
  customer_page,
  customer_update
} from '@/api/base-data/customer'
import SubmitModule from '@/views/base-data/customer/modules/submitModule.vue'
import detailModule from '@/views/base-data/customer/modules/detailModule.vue'

export default {
  name: 'Index',
  components: { SubmitModule, detailModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          customerName: '',
          phoneNum: '',
          state: ''
        }
      },
      dialogVisible: false,
      detailVisible: false
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      customer_page(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.doPage()
    },
    openOrClose(row) {
      this.tableLoading = true
      customer_update(row).then(resp => {
        if (resp.success) {
          this.$message.success('操作成功')
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 新增按钮点击
    saveClick() {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init()
      })
    },
    // 修改按钮点击
    updateClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(row.id)
      })
    },
    // 详情查看点击
    detailClick(row) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detailModule.init(row.id)
      })
    },
    handleBatchDelete(row) {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const data = { ids: this.selected.map(item => item.id) }
        customer_delete(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.doPage()
        })
      })
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.dialogVisible = false
      this.detailVisible = false
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      customer_delete(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
